package com.jjc.android.activity

import android.os.Bundle
import com.allen.library.SuperButton
import com.hjq.toast.ToastUtils
import com.jjc.android.Constants
import com.jjc.android.R
import com.jjc.android.base.BaseActivity
import com.jjc.android.entity.ForgetPasswordSuccessEvent
import com.jjc.android.entity.LzyResponse
import com.jjc.android.entity.UserInfo
import com.jjc.android.net.SimpleHttpCallback
import com.jjc.android.util.paramEx
import com.jjc.android.view.CommonKt
import com.jjc.android.view.CountDownTextView
import com.jjc.android.view.onClick
import com.jjc.android.view.textChange
import com.lzy.okgo.OkGo
import com.xw.repo.XEditText
import org.greenrobot.eventbus.EventBus

class ForgetPasswordActivity : BaseActivity(){

    override val layoutId: Int = R.layout.activity_reset_password
    private val okBtn: SuperButton by lazy { findViewById<SuperButton>(R.id.okBtn) }
    private val rePasswordEt: XEditText by lazy { findViewById<XEditText>(R.id.rePasswordEt) }
    private val passwordEt: XEditText by lazy { findViewById<XEditText>(R.id.passwordEt) }
    private val sendOtpBtn: CountDownTextView by lazy { findViewById<CountDownTextView>(R.id.sendOtpBtn) }
    private val smsCodeEt: XEditText by lazy { findViewById<XEditText>(R.id.smsCodeEt) }
    private val phoneEt: XEditText by lazy { findViewById<XEditText>(R.id.phoneEt) }

    override fun initView(savedInstanceState: Bundle?) {
        setActivityTitle(getString(R.string.forget_pwd_fix_txt))
        sendOtpBtn.isEnabled = true
        sendOtpBtn.onClick {
            if(phoneEt.text.toString().isNullOrEmpty()){
                ToastUtils.show(getString(R.string.input_phone_txt))
                return@onClick
            }
            if(!CommonKt.isPhoneValid(phoneEt.text.toString())){
                ToastUtils.show(getString(R.string.phone_format_error))
                return@onClick
            }
            OkGo.get<LzyResponse<String>>(Constants.captchaSmsApp)
                .params(mapOf("phonenumber" to phoneEt.textEx))
                .tag(this)
                .execute(object : SimpleHttpCallback<LzyResponse<String>>(activity = this){
                    override fun onCallSuccess(response: LzyResponse<String>) {
                        sendOtpBtn.startCountDown()
                    }
                })
        }
        okBtn.onClick {
            if(phoneEt.text.toString().isNullOrEmpty()){
                ToastUtils.show(getString(R.string.input_phone_txt))
                return@onClick
            }
            if(!CommonKt.isPhoneValid(phoneEt.text.toString())){
                ToastUtils.show(getString(R.string.input_right_phone_txt))
                return@onClick
            }
            if(smsCodeEt.text.isNullOrEmpty()){
                ToastUtils.show(getString(R.string.input_sms_code_txt))
                return@onClick
            }
            if(!CommonKt.isSmsCodeValid(smsCodeEt.text.toString())){
                ToastUtils.show(getString(R.string.input_right_sms_code_txt))
                return@onClick
            }
            if(passwordEt.text.toString().isNullOrEmpty()){
                ToastUtils.show(getString(R.string.input_pwd_txt))
                return@onClick
            }
            if(rePasswordEt.text.toString().isNullOrEmpty()){
                ToastUtils.show(getString(R.string.re_input_pwd_txt))
                return@onClick
            }
            if(!CommonKt.isPasswordValid(passwordEt.text.toString())){
                ToastUtils.show(getString(R.string.input_right_new_pwd_txt))
                return@onClick
            }
            if(!CommonKt.isPasswordValid(rePasswordEt.text.toString())){
                ToastUtils.show(getString(R.string.re_input_right_new_pwd_txt))
                return@onClick
            }
            if(passwordEt.text.toString()!=rePasswordEt.text.toString()){
                ToastUtils.show(getString(R.string.pwd_not_same_txt))
                return@onClick
            }
            OkGo.post<LzyResponse<String>>(Constants.updatePwd)
                .paramEx(mutableMapOf(
                    "username" to phoneEt.textEx,
                    "password" to passwordEt.textEx,
                    "confirmPassword" to rePasswordEt.textEx,
                    "code" to smsCodeEt.textEx
                ))
                .tag(this@ForgetPasswordActivity)
                .execute(object : SimpleHttpCallback<LzyResponse<String>>(activity = this){
                    override fun onCallSuccess(response: LzyResponse<String>) {
                        EventBus.getDefault().post(ForgetPasswordSuccessEvent())
                        ToastUtils.show(getString(R.string.reset_pwd_success_txt))
                        finish()
                    }
                })
        }
    }
}